const { Op } = require("sequelize");
const Sequelize = require('sequelize');
var COS = require('cos-nodejs-sdk-v5');
const Token = appRequire('common/token');
const wxMini = appRequire('common/wx/wxMini');

const userModel = appRequire('models/user');
const artPPTModel = appRequire('models/artPPT');
const downLoadModel = appRequire('models/downLoad');

// 导出对象，供其它模块调用
module.exports = {
    confirm,
    save,
    list
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['uid'], request);
    if (checkResult == false) return;

    let params = {
        userId: postData.uid
    }
    let orders = [['isNice', 'DESC'], ['downLoadNum', 'DESC'], ['createdAt', 'DESC']]
    if (postData.classId) params.ClassId = postData.classId
    //if (postData.typeId) params.TypeId = postData.typeId
    if (postData.order) {
        if (postData.order.indexOf("-") == -1) {
            orders = [[postData.order, 'ASC']]
        } else {
            orders = [[postData.order.split("-")[1], 'DESC']]
        }
    }
    if (postData.searchText) {
        params[Op.or] = [
            { title: { [Op.like]: '%' + postData.searchText + '%' } },
            { desc: { [Op.like]: '%' + postData.searchText + '%' } },
            { tags: { [Op.substring]: '%' + postData.searchText + '%' } },
            { fileUrls: { [Op.substring]: '%' + postData.searchText + '%' } },
        ]
    }
    if (postData.isNice) params.isNice = postData.isNice
    if (postData.age && postData.age != "年龄" && postData.age != "全部") params.age = postData.age
    if (postData.TypeId) params.TypeId = postData.TypeId
    let course = await artPPTModel.findAndCountAll({
        where: params,
        order: orders,
        limit: postData.pageSize,
        offset: postData.pageSize * (postData.pageNum - 1),
        include: ["typeDesc"]
    })
    course = Common.clone(course)
    course.rows = Common.transfromDataList(course.rows)
    
    returnData.maxCount = course.count
    returnData.maxPage = Math.ceil(course.count / postData.pageSize)
    returnData.data = course.rows
    request.json(returnData)
}

async function save(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['title', 'fileUrl'], request);
    if (checkResult == false) return;

    let params = {
        title: postData.title,
        content: postData.content,
        desc: postData.desc,
        price: postData.price,
        number: postData.number,
        tags: postData.tags,
        fileUrls: [{
            title: postData.title,
            desc: postData.desc,
            fileUrl: postData.fileUrl,
            page: 4,
            size: postData.size,
        }],
        age: postData.age,
        page: 4,
        userId: postData.uid,
        status: 0,
    }

    if (postData.classId) {
        params.ClassId = postData.classId
    } else {
        params.ClassId = null
    }
    if (postData.objectId) {
        let result = await artPPTModel.update(params, {
            where: {
                objectId: postData.objectId
            }
        })
        returnData.data = result
    } else {
        let thisResult = await artPPTModel.findOne({
            where: {
                title: postData.title,
            }
        })

        if (thisResult) {
            returnData.code = 0
            returnData.msg = "已存在此名称的PPt"
            return request.json(returnData)
        }

        params.objectId = sid.uuid()
        let result = await artPPTModel.create(params)
        returnData.data = result

        //if (postData.uid == "3h4SfJjFn9J4SHA" || postData.uid == "3dvLy3SCRVAFLpo" || postData.uid == "3gI2zAg9Zn8RJZ8") { //如果是管理员
        params.status = 0
    }

    request.json(returnData)
}

//确认成为
async function confirm(req, request) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let result1 = Common.checkUrl(postData, ['uid'], request);
    if (result1 == false) return;

    let result2 = await userModel.user.findOne({
        where: {
            objectId: postData.uid
        }
    })
    result2 = Common.clone(result2)

    if (result2.isCreater == 0) {
        returnData.data = Common.clone(result2)
        returnData.code = 0
        returnData.msg = "您已申请,请勿重复申请"
        return request.json(returnData)
    } else if (result2.isCreater == 1) {
        returnData.data = Common.clone(result2)
        returnData.code = 0
        returnData.msg = "您已是创作者,请勿重复申请"
        return request.json(returnData)
    } else {
        let result3 = await userModel.user.update({
            isCreater: 0
        }, {
            where: {
                objectId: postData.uid
            }
        })
        returnData.data = Common.clone(result3)
        return request.json(returnData)
    }
}
